const personRegister = new Vue({
    el: "#person-register",
    data: {
        username: "",
        password: "",
        password2: "",
        role: 0,
        telephone: "",
        email: "",
        checkEmailResult: false,
        checkUsernameResult: false,
        checkPasswordResult: false,
        checkTelephoneResult: false,
        checkBoxResult: false,
        checkCode: "",
        message: "",

        agreement: false
    },
    methods: {
        // 是否显示用户协议
        ShowAgreement: function() {
            this.agreement = true;
        },
        closeAgreement: function() {
            this.agreement = false;
        },
        //检查用户名是否重复
        repeatUsername: function() {
            if (this.username !== "") {
                axios.get("/user/repeatUsername/" + this.username).then(response => {
                    const resultInfo = response.data;
                    this.message = resultInfo.message;
                    layer.msg(this.message, { time: 1500 });
                    this.checkUsernameResult = true;
                }).catch();
            } else {
                this.message = "用户名不能为空";
                layer.msg(this.message, { time: 1500 });
            }
        },

        //检验密码是否一致  
        checkPassword: function() {
            if (this.password2 !== this.password) {
                this.message = "密码不一致";
                layer.msg(this.message, { time: 1500 });
            }
            this.checkPasswordResult = true;
        },

        //检查手机号是否已经被使用
        checkTelephone: function() {

            //手机号正则表达式
            const re = /^1\d{10}$/;

            if (this.telephone !== "") {
                if (re.test(this.telephone)) {
                    axios.get("/user/repeatTelephone/" + this.telephone).then(response => {
                        const resultInfo = response.data;
                        this.message = resultInfo.message;
                        layer.msg(this.message, { time: 1500 });
                        this.checkTelephoneResult = true;
                    }).catch();
                } else {
                    this.message = "手机号不合法，请检查是否正确";
                    layer.msg(this.message, { time: 1500 });
                }

            } else {
                this.message = "手机号不能为空";
                layer.msg(this.message, { time: 1500 });
            }

        },


        //检查邮箱是否已经被使用
        checkEmail: function() {

            //邮箱正则表达式
            const re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;

            if (this.email !== "") {
                if (re.test(this.email)) {
                    axios.get("/user/repeatEmail?email=" + this.email).then(response => {
                        const resultInfo = response.data;
                        this.message = resultInfo.message;
                        layer.msg(this.message, { time: 1500 });
                    }).catch();
                    this.checkEmailResult = true;
                } else {
                    this.message = "邮箱不合法，请检查是否正确";
                    layer.msg(this.message, { time: 1500 });
                }
            } else {
                this.message = "邮箱不能为空";
                layer.msg(this.message, { time: 1500 });
            }

        },

        //检查是否已经勾选
        checkBox: function() {
            if (this.checkBoxResult === false) {
                this.message = "请勾选我已认真阅读《用户协议》"
                layer.msg(this.message, { time: 1500 });
            }
            console.log(this.checkBoxResult)
        },

        //注册
        register: function() {

            console.log(this.checkUsernameResult, this.checkPasswordResult, this.checkTelephoneResult, this.checkEmailResult, this.checkBoxResult);

            if (this.checkUsernameResult === true && this.checkPasswordResult === true && this.checkTelephoneResult === true &&
                this.checkEmailResult === true && this.checkBoxResult === true) {
                //layui加载框
                const load = layer.load({ time: 2000 });
                axios.post("/user/registration", {
                    username: this.username,
                    password: this.password,
                    role: this.role,
                    telephone: this.telephone,
                    email: this.email,
                    checkCode: this.checkCode
                }).then(response => {

                    //请求响应回来就进行关闭
                    layer.close(load);

                    const resultInfo = response.data;
                    this.message = resultInfo.message;

                    //如果注册成功，status===1 ，直接跳转登录页面
                    if (resultInfo.status === 1) {
                        layer.msg(this.message, { time: 2000 });
                        location.href = "/login.html";
                    }
                }).catch()
            } else {
                this.message = "注册信息不完整，请仔细检查"
                layer.msg(this.message, { time: 1500 });
            }

        },


    },
})